package com.wyz.controller;

import com.wyz.entity.Teacher;
import com.wyz.entity.Testpaper;
import com.wyz.service.TestPaperService;
import com.wyz.service.impl.TeacherServiceImpl2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 教师端的所有操作控制器
 */
@Controller
public class TeacherAllController {
    @Autowired
    TeacherServiceImpl2 dao;

    /**
     * 登录页面
     *
     * @return
     */
    @RequestMapping("/login.teacher")
    public String teacherLogin() {
        return "teacher/login";
    }

    /**
     * 进入首页
     *
     * @param teacher
     * @param pwd
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/index.teacher")
    public String teacherIndex(String teacher, String pwd, Model model, HttpSession session) {
        Teacher tea = dao.getLogin(teacher, pwd);
        if (tea != null) {
            model.addAttribute("teacher", tea);
            session.setAttribute("teacher", tea);
            return "teacher/index";
        } else {
            return "teacher/login";
        }
    }
    @Autowired
    TestPaperService testPaperService;
    /**
     * 点击查看,由于试题是死的，我们可以将他放入session中方便直接读取
     * @return
     */
    @RequestMapping("/testpaper")
    public String TestPaper(Model model,HttpSession session){

        List<Testpaper> list=testPaperService.getNameInfo("",1);
        for(int i=0;i<list.size();i++){
            Testpaper testpaper=(Testpaper) list.get(i);
            String[] act=testpaper.getTestTitle().split(",");
            list.get(i).setB(act.length+"");
        }
        model.addAttribute("testPaper",list);
        session.setAttribute("testPaperAll",list);
        return "teacher/testpaper";
    }
    @RequestMapping("/getTiaojInfo")
    public String getTiaojInfo(String name,String openDate,String closeDate,Model model,HttpSession session){
        System.out.println(name+"\n"+openDate+"\n"+closeDate);

        if(name!=null || openDate!=null || closeDate!=null){
            List<Testpaper> list=testPaperService.getOpenDateInfo(name,openDate,closeDate);
            for(int i=0;i<list.size();i++){
                Testpaper testpaper=(Testpaper) list.get(i);
                String[] act=testpaper.getTestTitle().split(",");
                list.get(i).setB(act.length+"");
            }
            model.addAttribute("testPaper",list);
        }else{
            model.addAttribute("testPaper",session.getAttribute("testPaperAll"));
        }

        return "teacher/testpaper";
    }
    @RequestMapping("/delete")
    public String deleteInfo(int id,Model model,HttpSession session){
        testPaperService.delete(id);
        List<Testpaper> list=testPaperService.getNameInfo("",1);
        for(int i=0;i<list.size();i++){
            Testpaper testpaper=(Testpaper) list.get(i);
            String[] act=testpaper.getTestTitle().split(",");
            list.get(i).setB(act.length+"");
        }
        model.addAttribute("testPaper",list);
        session.setAttribute("testPaperAll",list);
        return "teacher/testpaper";
    }
    public String getIdInfo(){
        return "";
    }
}

  